﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DATA
{
    public class ContactREP
    {
         
        MODELGENERALDataContext context = new MODELGENERALDataContext();
        
     
        public List<Contact> GetAllContacts()
        {
            List<Contact> acc = new List<Contact>();

            var acounts = from a in context.Contacts
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public List<Contact> GetActiveContact()
        {
           List<Contact> acc = new List<Contact>();

            var acounts = from a in context.Contacts
                        
                          select a;
            foreach (var account in acounts)
            {
                acc.Add(account);
            }
            return acc;
        }

        public bool CheckDuplicate(int AccountID)
        {
            Contact newacc = new Contact();

            for (int i = 0; i < (int)(context.Contacts.Count()); i++)
            {
                Contact acc = context.Contacts.Single(c => c == newacc);
                if (AccountID.Equals(acc.contactID))
                {
                    return true;
                }



               
            }
            return false;
        }

        public bool  AddContact(Contact newAccount)
        {
            Contact newacc = new Contact();
            Contact acc = context.Contacts.Single(c=>c ==newacc);
            if (CheckDuplicate(newAccount.contactID) == false)
            {
                context.Contacts.InsertOnSubmit(acc);

                context.SubmitChanges();
            }
            
            return true;
             
        }
        public void deleteContact(int AccountID)
        {
            var acc = from c in context.Contacts
                      where c.contactID == AccountID 
                      select c;

            context.Contacts.DeleteAllOnSubmit(acc);
            context.SubmitChanges();

 
        }

        public bool UpdateContact(Contact accountup)
        {
            try
            {
                Contact acc = context.Contacts.Single(
                                                        nv => nv.contactID == accountup.contactID
                                                        );
                acc.subject = accountup.subject;
                acc.phone = accountup.phone;
                acc.email = accountup.email;
                acc.content = accountup.content;
                acc.contactName = accountup.contactName;
               acc.email = accountup.email;
                acc.address = accountup.address;
                
              
                context.SubmitChanges();
            }

             catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    
    }
}
